﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;

namespace ModuleDuoc.DM
{
    public partial class DM_DonMau : Form
    {

        string _manhanvien = "Nguyễn Văn Hiển";

        public DM_DonMau()
        {
            InitializeComponent();
        }
        public DM_DonMau(string tennhanvien)
        {
            InitializeComponent();
            _manhanvien = tennhanvien;
        }
        // khai báo 1 hàm delegate
        public delegate void GetString(String MyString1);
        // khai báo 1 kiểu hàm delegate
        public GetString MyGetData;

        private void setTab(Control c, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                c.Focus();
            }
        }

        private void DM_DonMau_Load(object sender, EventArgs e)
        {
            this.lblTenBS.Text = _manhanvien;
            fillCboxSP();
            this.dgvLeft.AutoGenerateColumns = false;
            this.loadDonMau();
        }

        private void fillCboxSP()
        {
            try
            {
                BC.ZD_DM_THUOCBC bc = new BC.ZD_DM_THUOCBC();
                this.cboxSP.DataSource = bc.GetAll();
                this.cboxSP.DisplayMember = "TenVT";
                this.cboxSP.ValueMember = "MaVT";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void loadDonMau()
        {
            string sql = "SELECT DonMau_ID, TenDonMau FROM zD_DonMau zdm WHERE zdm.BacSi = N'" + _manhanvien + "' ORDER BY zdm.DonMau_ID desc";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvLeft.DataSource = dt;
            this.btnSoLuong.Text = "Số lượng:" + dt.Rows.Count;
        }

        private void txtTenDonMau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtChanDoan, e);
            }
        }

        private void txtChanDoan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.txtLoiDan, e);
            }
        }

        private void txtLoiDan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.cboxSP, e);
            }
        }

        private void cboxSP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.checkInput() == true)
                {
                    setTab(this.txtSoLuong, e);
                }
            }
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                setTab(this.cboxLieuDung, e);
            }
        }

        private void cboxLieuDung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChon_Click(sender, e);
            }
            setTab(this.cboxSP, e);
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            this.dgvData.Rows.Add(this.cboxSP.Text + " (" + this.txtHoatChat.Text + ")", this.txtDVT.Text, this.txtSoLuong.Text, this.cboxLieuDung.Text);
            if (this.txtDonMau_ID.Text != "")
            {
                int i = this.dgvData.Rows.Count - 1;
                BC.ZD_DonMauCTBC bc = new BC.ZD_DonMauCTBC();
                BE.ZD_DonMauCT be = new BE.ZD_DonMauCT();

                be.DonMau_ID = Int32.Parse(dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString());
                be.TenVT = dgvData.Rows[i].Cells["tenvt"].Value.ToString();
                be.DonViTinh = dgvData.Rows[i].Cells["dvt"].Value.ToString();
                be.SoLuong = Int32.Parse(dgvData.Rows[i].Cells["soLuong"].Value.ToString());
                be.LieuDung = dgvData.Rows[i].Cells["LieuDung"].Value.ToString();

                bc.Insert(be);
                this.dgvData.Rows.Clear();
                clickDgvLeft_DonMau();
                clickDgvLeft_DonMauCT();  
            }         
        }

        private void cboxSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtSoLuong.Text = "";
            if (this.cboxSP.Text != "")
            {
                string sql = "SELECT * FROM hien_Thuoc_dvt zdt WHERE zdt.TENVT = N'" + this.cboxSP.Text.ToString() + "'";
                DataTable dt = new DataTable();
                dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    this.txtDVT.Text = dt.Rows[0]["ten"].ToString();
                    this.cboxLieuDung.Text = dt.Rows[0]["Usage"].ToString();
                    this.txtHoatChat.Text = dt.Rows[0]["Infoinclude"].ToString();
                }              
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //if (this.txtDonMau_ID.Text != "")
            {
                if (MessageBox.Show("Bạn có muốn xóa thuốc này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        BC.ZD_DonMauCTBC bc = new BC.ZD_DonMauCTBC();
                        bc.Delete(BE.ZD_DonMauCT.ZD_DonMauCTFields.DonMauCT_ID, this.dgvData.Rows[this.dgvData.CurrentRow.Index].Cells["id"].Value.ToString());
                        //MessageBox.Show("Xóa thành công !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception)
                    { }

                    for (int i = this.dgvData.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        this.dgvData.Rows.RemoveAt(this.dgvData.SelectedRows[i].Index);
                    }
                }
            }
        }

        private bool checkInput()
        {
            if (this.txtTenDonMau.Text == "" || this.txtTenDonMau.Text.Length > 100)
            {
                MessageBox.Show("Không được để trống hoặc dài quá!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtTenDonMau.Focus();
                return false;
            }
            else if (this.txtChanDoan.Text == "" || this.txtChanDoan.Text.Length > 300)
            {
                MessageBox.Show("Không được để trống hoặc dài quá!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtChanDoan.Focus();
                return false;
            }
            else if (this.txtLoiDan.Text == "" || this.txtLoiDan.Text.Length > 200)
            {
                MessageBox.Show("Không được để trống hoặc dài quá!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.txtLoiDan.Focus();
                return false;
            }
            return true;
        }

        private void btnLuuDonMau_Click(object sender, EventArgs e)
        {
            if (this.txtDonMau_ID.Text == "")
            {
                if (this.dgvData.Rows.Count > 0 && this.txtTenDonMau.Text != "")
                {
                    BE.ZD_DonMau be = new BE.ZD_DonMau();
                    BC.ZD_DonMauBC bc = new BC.ZD_DonMauBC();

                    be.TenDonMau = this.txtTenDonMau.Text;
                    be.ThoiGian = DateTime.Now;
                    be.BacSi = this.lblTenBS.Text;
                    be.ChanDoan = this.txtChanDoan.Text;
                    be.LoiDan = this.txtLoiDan.Text;

                    bc.Insert(be);
                    insertDonMauCT();
                    MessageBox.Show("Lưu đơn thuốc mẫu thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.loadDonMau();
                    this.txtDonMau_ID.Text = dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString();
                }
            }
            else
            {
                BE.ZD_DonMau be = new BE.ZD_DonMau();
                BC.ZD_DonMauBC bc = new BC.ZD_DonMauBC();

                be.DonMau_ID = Int32.Parse(dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString());
                be.TenDonMau = this.txtTenDonMau.Text;
                be.ThoiGian = DateTime.Now;
                be.BacSi = this.lblTenBS.Text;
                be.ChanDoan = this.txtChanDoan.Text;
                be.LoiDan = this.txtLoiDan.Text;

                bc.Update(be);
                MessageBox.Show("Đơn thuốc được lưu thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.loadDonMau();
            }
        }
        private void insertDonMauCT()
        {
            try
            {
                BE.ZD_DonMauCT be = new BE.ZD_DonMauCT();
                BC.ZD_DonMauCTBC bc = new BC.ZD_DonMauCTBC();
                DataTable dt = new DataTable();

                for (int i = 0; i < this.dgvData.Rows.Count; i++)
                {
                    be.DonMau_ID = Int32.Parse(getLast_DonMauID());
                    be.TenVT = this.dgvData.Rows[i].Cells[0].Value.ToString();
                    be.SoLuong = Int32.Parse(this.dgvData.Rows[i].Cells[2].Value.ToString());
                    be.DonViTinh = this.dgvData.Rows[i].Cells[1].Value.ToString();
                    be.LieuDung = this.dgvData.Rows[i].Cells[3].Value.ToString();
                    bc.Insert(be);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lỗi thêm chi tiết đơn thuốc!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string getLast_DonMauID()
        {
            string sql = "SELECT TOP 1 DonMau_ID FROM zD_DonMau zdm ORDER BY zdm.DonMau_ID desc";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            return dt.Rows[0]["DonMau_ID"].ToString();
        }

        

        private void dgvLeft_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                this.dgvData.Rows.Clear();
                clickDgvLeft_DonMau();
                clickDgvLeft_DonMauCT();
            }
            catch (Exception)
            { }
        }

        private void clickDgvLeft_DonMau()
        {
            string sql = "SELECT * FROM zD_DonMau zdm WHERE zdm.DonMau_ID = '" + dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString() + "'";

            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.txtTenDonMau.Text = dt.Rows[0]["TenDonMau"].ToString();
            this.lblTenBS.Text = dt.Rows[0]["BacSi"].ToString();
            this.txtChanDoan.Text = dt.Rows[0]["ChanDoan"].ToString();
            this.txtLoiDan.Text = dt.Rows[0]["LoiDan"].ToString();
            this.txtDonMau_ID.Text = dt.Rows[0]["DonMau_ID"].ToString();            
        }
        private void clickDgvLeft_DonMauCT()
        {
            string sql = "SELECT * FROM zD_DonMauCT zdmc WHERE zdmc.DonMau_ID = '" + dgvLeft.Rows[this.dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString() + "'";
            DataTable dt = new DataTable();
            dt = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.dgvData.Rows.Add(dt.Rows[i]["TenVT"].ToString(), dt.Rows[i]["DonViTinh"].ToString(), dt.Rows[i]["SoLuong"].ToString(), dt.Rows[i]["LieuDung"].ToString(), dt.Rows[i]["DonMauCT_ID"].ToString());
            }
        }

        private void dgvLeft_KeyDown(object sender, KeyEventArgs e)
        {
            clickDgvLeft_DonMau();
            clickDgvLeft_DonMauCT();
        }

        private void dgvLeft_KeyUp(object sender, KeyEventArgs e)
        {
            clickDgvLeft_DonMau();
            clickDgvLeft_DonMauCT();
        }

        private void btnXoaDonMau_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(dgvData.Rows.Count.ToString());
            if (this.txtDonMau_ID.Text != "")
            {
                if (MessageBox.Show("Bạn có muốn xóa đơn thuốc này không?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        for (int i = 0; i < this.dgvData.Rows.Count; i++)
                        {
                            BC.ZD_DonMauCTBC bc = new BC.ZD_DonMauCTBC();
                            bc.Delete(BE.ZD_DonMauCT.ZD_DonMauCTFields.DonMauCT_ID, this.dgvData.Rows[i].Cells["id"].Value.ToString());
                        }
                        BC.ZD_DonMauBC bc1 = new BC.ZD_DonMauBC();
                        bc1.Delete(BE.ZD_DonMau.ZD_DonMauFields.DonMau_ID, this.dgvLeft.Rows[dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString());
                    }
                    catch (Exception)
                    { }
                    this.dgvData.Rows.Clear();
                    MessageBox.Show("Xóa thành công!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.loadDonMau();
                }
            } 
        }

        private void btnChonDonMau_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {// tại đây gọi nó
                MyGetData(dgvLeft.Rows[dgvLeft.CurrentRow.Index].Cells["DonMau_ID"].Value.ToString());
            }
            this.Close();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.txtDonMau_ID.Text = "";
            this.txtTenDonMau.Text = "";
            this.txtChanDoan.Text = "";
            this.txtLoiDan.Text = "";
            dgvData.Rows.Clear();
        }
    }
}
